package com.bhs.sansonglogistics.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.bean.BankCardList;
import com.bhs.sansonglogistics.ui.login.RealNameAuthenticationActivity;
import com.bhs.sansonglogistics.ui.wallet.AddCardActivity;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.SharedPreferencesUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import java.util.List;

/**
 * 选择银行卡
 */
public class SelectBankCardDialog extends Dialog {

    private RecyclerView mRvList;
    private BaseQuickAdapter<BankCardList, BaseViewHolder> mAdapter;
    private List<BankCardList> list;

    public SelectBankCardDialog(@NonNull Context context, List<BankCardList> list) {
        super(context, R.style.MyBottomDialogStyle);
        this.list = list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_bottom_select_bank_card);
        Window window = getWindow();
        //底部弹出的Dialog
        window.setGravity(Gravity.BOTTOM);
        //设置动画
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams attributes = window.getAttributes();
        //保证按钮水平满屏
        attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
        attributes.height = MyUtils.getScreenHeight(getContext()) * 7 / 10;
        //设置显示位置
        window.setAttributes(attributes); //将属性设置给窗体
        initView();
        mAdapter = new BaseQuickAdapter<BankCardList, BaseViewHolder>(R.layout.item_bankcard, list) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, BankCardList item) {
                String bank_number = item.getBank_number();
                bank_number = bank_number.substring(bank_number.length() - 4);
                helper.setText(R.id.tv_card_type, String.format("%s %s (%s)", item.getBank_name(),
                        MyUtils.getCNCardType(item.getBank_type()), bank_number));
                helper.setGone(R.id.iv_tick, item.isChecked());
            }
        };
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (BankCardList bankCardList : list) {
                    bankCardList.setChecked(false);
                }
                list.get(position).setChecked(true);
                mAdapter.notifyDataSetChanged();
                BankCardList info = list.get(position);
                String bank_number = info.getBank_number();
                bank_number = bank_number.substring(bank_number.length() - 4);
                myClickListener.onClick(String.valueOf(info.getBank_id()), String.format("%s(%s)", info.getBank_name(), bank_number));
                dismiss();
            }
        });
        View view=LayoutInflater.from(getContext()).inflate(R.layout.layout_empty, null);
        mAdapter.setEmptyView(view);
        TextView btnHistory=view.findViewById(R.id.btn_history);
        btnHistory.setVisibility(View.VISIBLE);
        btnHistory.setText("去添加");
        btnHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                voucher();
            }
        });

    }
    private void voucher() {
        boolean is_approve = SharedPreferencesUtils.getBoolean(getContext(), "is_approve", false);
        if (is_approve) {
            getContext().startActivity(new Intent(getContext(), AddCardActivity.class).putExtra("bank_type", 1));
        } else {
            new XPopup.Builder(getContext())
                    .asConfirm("提示", "你还未实名认证，现在去认证",
                            "取消", "确定",
                            new OnConfirmListener() {
                                @SuppressLint("NotifyDataSetChanged")
                                @Override
                                public void onConfirm() {
                                    getContext().startActivity(new Intent(getContext(), RealNameAuthenticationActivity.class));
                                }
                            }, null, false)
                    .show();
        }
    }
    private MyClickListener2 myClickListener;

    public void setMyClickListener(@Nullable MyClickListener2 l) {
        this.myClickListener = l;
    }

    private void initView() {
        findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("选择银行卡");
        mRvList = findViewById(R.id.rv_list);
    }
}
